/**
 * Created by Churcy on 2017/1/10.
 */
window.onload = function() {
	Inner.searchInner();
}

var Inner = {

	searchInner : function() {
		$("#searchBTN").on('click', function() {
			var conditions = getCurrentConditions();
			conditions.pageNo = 1;
			ajaxSearchInnerList(conditions);
		});
		$("#resetBTN").on('click', function() {
			resetFn();
		});
	}
}
function searchFn() {
	var conditions = getCurrentConditions();
	console.log("searchFN")
	var response = ajaxSearchInnerList(conditions);
}
function resetFn() {
	
	$("#vehNo").val("");
	$("#deviceId").val("");
	$("#telephone").val("");
	$('select[name="note"]').prop('selectedIndex', 0);
}

function getCurrentConditions() {
	console.log("getCurrentConditions");
	var vehNo = $("#vehNo").val();
	var deviceId = $("#deviceId").val();
	var telephone = $("#telephone").val();
	var pageSize = $("#n").val();
	var pageNo = $("#p").val();
	var appKey = $("#APP_KEY").val();
	
	console.log("val取值:" + status);
	var conditions = {
		vehNo : encodeURI(vehNo),
		deviceId : encodeURI(deviceId),
		telephone : telephone,
		pageIndex : pageNo,
		appKey : appKey
	};
	return conditions;

}
function ajaxSearchInnerList(conditions) {
	console.log("ajaxSearchInnerList");
	$.ajax({
		type : "GET",
		url : PROJECT_CTX + "/trafficback/appListInner",
		data : conditions,
		dataType : "json",
		success : function(data) {
			console.log(data)
			var result = eval(data);
			var resultCode = result.status.code;
			console.log(resultCode)
			console.log(result.line);
			if (1 == resultCode) {
				// 清空当前数据
				var innerList = $("#innerList").empty();
				// 填写查询出来的数据
				$.each(result.line, function(i, item) {
					var tr = "<tr>";
					
					tr += "<td >";
					tr += item.vehNo;
					tr += "</td>"

					tr += "<td >";
					tr += item.vehCode;
					tr += "</td>"
						
					tr += "<td >";
					tr += item.address;
					tr += "</td>"

					tr += "<td >";
					tr += item.createTime;
					tr += "</td>"

					tr += "<td >";
					tr += item.deviceId;
					tr += "</td>"
						
					tr += "<td >";
					tr += item.note;
					tr += "</td>"
						
					tr += "<td >";
					tr += item.noteDetails;
					tr += "</td>"
						
					tr += "<td >";
					tr += item.telephone;
					tr += "</td>"

					tr += "<td >";
					tr += " <a class='table-link primary' title='修改' href='"+ PROJECT_CTX + "/traffic/app/inner/info?id="+ item.id + "&appKey=" + conditions.appKey+ "'>修改</a>";
					tr += "</td >";

					tr += "</tr>";
					innerList.append(tr);
					// 更新分页栏
					reloadPage(result);
				});
			}

		}

		,
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			console.log(XMLHttpRequest.status);
			console.log(XMLHttpRequest.readyState);
			console.log(textStatus);
		}
	});
}


